/*
 * Copyright (c) 2012 Aleksander Zotov, Benedikt Markt, Aliaksei Babkovich
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 *  of this software and associated documentation files (the "Software"),
 *  to deal in the Software without restriction, including without limitation
 *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
 *  and/or sell copies of the Software, and to permit persons to whom the
 *  Software is furnished to do so, subject to the following conditions:
 *  
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 *  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 *  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 *  DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 *  OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
 *  THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package user;

import java.util.Locale;

import database.DatabaseManager;
import utils.*;

/**
 * This class represents an user entity registered in SatPro
 * 
 * @author Benedikt Markt
 * @see UserManager
 * TODO remove UserPreferences class from draft
 */

public class User {

	private static final int PW_LENGTH = 12;

	private String name;
	private String password;
	private Boolean isAdmin = false;

	private Locale language;
	private String referenceSystem;
	
	private Boolean hasRunningSim = false;

	public User(String name) throws InvalidInputException {
		if (Validator.isValidUsername(name)) {
			this.name = name;
		} else {
			throw new InvalidInputException("Invalid Username");
		}

		this.password = PasswordGenerator.generate(PW_LENGTH);
		language = Locale.ENGLISH;
		referenceSystem = "84";
	}
	
	public User(String name, String password, Boolean isAdmin, Locale language, String referenceSystem) {
		this.name = name;
		this.password = password;
		this.isAdmin = isAdmin;
		this.language = language;
		this.referenceSystem = referenceSystem;
	}

	public Locale getLanguage() {
		return language;
	}

	public String getName() {
		return this.name;
	}

	public String getPassword() {
		return this.password;
	}

	// TODO add to draft
	public String getReferenceSystem() {
		return referenceSystem;
	}

	public Boolean isAdmin() {
		return this.isAdmin;
	}
	
	public Boolean hasRunningSim() {
		return hasRunningSim;
	}

	public void resetPassword() {
		this.password = PasswordGenerator.generate(PW_LENGTH);

		// god please forgive me for i used this ugly hack
		DatabaseManager dbMan = new DatabaseManager();
		dbMan.storeUser(this);

	}
	

	public void setAdminState(Boolean isAdmin) {
		this.isAdmin = isAdmin;
		// god please forgive me for i used this ugly hack
		DatabaseManager dbMan = new DatabaseManager();
		dbMan.storeUser(this);
	}

	// TODO add to draft
	public void setLanguage(Locale language) {
		this.language = language;
		DatabaseManager dbMan = new DatabaseManager();
		dbMan.storeUser(this);
	}

	public void setPassword(String password) throws InvalidInputException {
		if (Validator.isValidPassword(password)) {
			this.password = password;
		} else {
			throw new InvalidInputException("Invalid Password");
		}
		DatabaseManager dbMan = new DatabaseManager();
		Debug.log(Debug.TRACE, "Setting password for user " + this.name);
		dbMan.storeUser(this);
	}

	// TODO add to draft
	public void setReferenceSystem(String referenceSystem) {
		this.referenceSystem = referenceSystem;
		DatabaseManager dbMan = new DatabaseManager();
		dbMan.storeUser(this);
	}
	
	public void setRunningSim(boolean hasRunningSim) {
		this.hasRunningSim = hasRunningSim;
		Debug.log(Debug.TRACE, "User " + this.name + " hasRunningSim = " + this.hasRunningSim);
	}
}
